<?php

namespace App\Http\Controllers;

use Artisan;
use App\Models\Sync;
use App\Models\Transaction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage;

class TransactionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $transactions = DB::table('transactions')->where('value', '>', 0);

        if ($request->from) {
            $transactions = $transactions->where(function($query) use ($request) {
                $query->where('from', $request->from)
                        ->orWhere('to', $request->from);
            });
        }

        if ($request->start_date) {
            $transactions = $transactions->where('timeStamp', '>=', strtotime($request->start_date));
        }

        if ($request->end_date) {
            $Transactions = $transactions->where('timeStamp', '<=', strtotime($request->end_date));
        }

        if ($request->type == 1) {
            $transactions = $transactions->where('input', '0x');
        } elseif ($request->type == 2) {
            $transactions = $transactions->where('input', '!=', '0x');
        }

        $transactions = $transactions->orderBy('timeStamp', 'desc')->paginate(15);

        $query_string = Input::only(['from', 'type']);

        $transactions->appends($query_string);

        $last_sync = Sync::where('type', 'transaction')->orderBy('created_at', 'desc')->first();

        return view('transaction.index', ['transactions' => $transactions, 'last_sync' => $last_sync]);
    }

    public function create()
    {
        if (Storage::exists('ethereum/addresses.json')) {
            $addresses = json_decode(Storage::get('ethereum/addresses.json'), true);    
        } else {
            $addresses = [];
        }       

        return view('transaction.create', ['addresses' => $addresses]);
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'address' => 'required|size:42',
        ]);

        $addresses_json_file = 'ethereum/addresses.json';

        if (Storage::exists($addresses_json_file)) {
            $addresses = json_decode(Storage::get($addresses_json_file), true);
            if (!in_array($request->address, $addresses)) {
                $addresses[] = $request->address;
            }

            Storage::put($addresses_json_file, json_encode($addresses));
        } else {
            Storage::put($addresses_json_file, json_encode([
                $request->address
            ]));
        }

        return redirect()->route('transaction.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $transaction = Transaction::find($id);
        return view('transaction.show', ['transaction' => $transaction]);
    }

    
}
